package com.example.module.ai.tongyi;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.example.framework.websocket.core.listener.WebSocketMessageListener;
import com.example.framework.websocket.core.sender.WebSocketMessageSender;
import com.example.module.ai.api.AiApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Slf4j
@Component
public class TongYiWebSocketMessageListener implements WebSocketMessageListener<JSONObject> {

    @Resource
    private WebSocketMessageSender webSocketMessageSender;

    @Resource(name = "tongYiAiApiImpl")
    private AiApi aiApi;

    @Override
    public void onMessage(WebSocketSession session, JSONObject message) {
        log.info("DemoWebSocketMessageListener 收到消息，session={}, message={}", session, message);
        String question = message.getStr("question");
        String answer = "你好，我是测试消息";
        if (ObjectUtil.isNotEmpty(question)) {
            answer = aiApi.completion(question);
        }
        webSocketMessageSender.sendBySessionId(session.getId(), getType(), answer);
    }

    @Override
    public String getType() {
        // {
        //    "type":"tongyi",
        //    "content": "{\"question\":\"你好\"}"
        // }
        return "tongyi";
    }
}
